package com.sinodata.bsm.cicp.collector.common;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.ProtocolParameterUtil;
import com.sinodata.bsm.common.constants.DataTypeConstants;
import com.sinodata.bsm.common.vo.ProtocolParameter;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: Ftp仿真采集器
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-11-12 14:40:03      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class FtpAgreement extends Collector {

    private static final Logger logger = Logger.getLogger(FtpAgreement.class);

    private static long time;

    public FtpAgreement() {
        super();
    }

    /**
     * 采集所有指标数据的方法
     * 用传进的属性参数进行尝试性登录.
     * 
     * @param String ip
     * @return List<ResPropertyValueX>成败标志||登录耗时
     * @throws CollectorException 
     */
    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        ProtocolParameter protocolParameter = ProtocolParameterUtil.getHostValue(ip);
        String userName = protocolParameter.getUserName();
        String password = protocolParameter.getPwd();
        boolean message;
        String newIp = this.paramMap.get("IP Address");//IP地址
        if (newIp != null && newIp.trim().length() > 0) {
            ip = newIp;
        }
        message = ftpLogin(userName, password, ip);
        if (message == true) {
            //propValues[0] = ResPropertyValue.createPropValue("YES", propMetas[0]);
            propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_TRUE, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(String.valueOf(time), propMetas[1]);
        } else {
            //propValues[0] = ResPropertyValue.createPropValue("NO", propMetas[0]);
            propValues[0] = ResPropertyValue.createPropValue(DataTypeConstants.BOOLEAN_VALUE_FALSE, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(null, propMetas[1]);
        }
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

    //检测ftp命令连接和登录主机的方法;
    private boolean ftpLogin(String userName, String password, String ip) throws CollectorException {
        time = System.currentTimeMillis();
        FTPClient ftp = new FTPClient();
        try {
            //根据参数“ip”，用ftp命令连接主机;
            ftp.connect(ip);
            ftp.setDataTimeout(15 * 1000);
        } catch (SocketException e) {
            logger.error("failed to ftp login", e);
            throw new CollectorException(e);
        } catch (IOException e) {
            logger.error("failed to ftp login", e);
            throw new CollectorException(e);
        } catch (Exception e) {
            logger.error("failed to ftp login", e);
            throw new CollectorException(e);
        }
        String[] reply = null;
        try {
            //检测ftp登录是否正常;
            ftp.login(userName, password);
            time = System.currentTimeMillis() - time;
            reply = ftp.getReplyStrings();
            ftp.setDataTimeout(15 * 1000);
        } catch (IOException e) {
            logger.error("failed to ftp login", e);
            throw new CollectorException(e);
        } catch (Exception e) {
            logger.error("failed to ftp login", e);
            throw new CollectorException(e);
        } finally {
            try {
                ftp.disconnect();
            } catch (IOException e) {
                logger.error("failed to disconnect FTPClient", e);
            }
        }
        //判断登录结果;
        for (int i = 0; i < reply.length; i++) {
            if (reply[i].startsWith("530")) {
                return false;
            }
        }
        return true;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "IP Address" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "连接系统", "连接耗时" };
        return new String[] { "Whether_Connect", "Connection_Time" };
    }

}
